﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Storage.EntityModels;

namespace GUI
{
    public partial class userSearch : TripLOA.GUI.internationalization.LOABasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// Retrieves all users that satisfy the search parameter(NickName, Gender, Smoker)
        /// and initialize the IEnumerable<Storage.EntityModels.User> users list.
        /// If no user is founded the list is empty.
        /// </summary>
        protected void DataBinding()
        {
            Search.UserParameter nickname = Search.UserParameter.Create<string>("Username", n => n.Contains(txtUserNick.Text));
            //Storage.EntityModels.Gender tmpGender;
            //if( ddlGender.SelectedValue.Equals('F')) tmpGender = Storage.EntityModels.Gender.Female;
            //else tmpGender = Storage.EntityModels.Gender.Male;
            Search.UserParameter gender;
            Search.UserParameter smoker;
            List<Search.UserParameter> paramz = new List<Search.UserParameter>();

            switch (rblSmoker.SelectedIndex)
            {
                case 0: smoker = Search.UserParameter.Create<byte>("Smoker", n => n == (byte)Smoker.Smoker);
                    paramz.Add(smoker);
                    break;
                case 1: smoker = Search.UserParameter.Create<byte>("Smoker", n => n == (byte)Smoker.NotSmoker);
                    paramz.Add(smoker);
                    break;
                case 2: smoker = Search.UserParameter.Create<byte>("Smoker", n => n == (byte)Smoker.Unknown);
                    break;
                default: smoker = Search.UserParameter.Create<byte>("Smoker", n => n == (byte)Smoker.Unknown);
                    break;
            }

            switch (rblGender.SelectedIndex)
            {
                case 0: gender = Search.UserParameter.Create<byte>("Gender", n => n == (byte)Gender.Male);
                    paramz.Add(gender);
                    break;
                case 1: gender = Search.UserParameter.Create<byte>("Gender", n => n == (byte)Gender.Female);
                    paramz.Add(gender);
                    break;
                case 2: gender = Search.UserParameter.Create<byte>("Gender", n => n == (byte)Gender.Unknown);
                    break;
                default: gender = Search.UserParameter.Create<byte>("Gender", n => n == (byte)Gender.Unknown);
                    break;
            }

            
            paramz.Add(nickname);
            IEnumerable<Storage.EntityModels.User> users = Search.SearchManager.UserSearch(paramz);
            gv_result.DataSource = users;
            gv_result.DataBind();
        }


        /// <summary>
        /// This method manage the search button click event.
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            content.Visible = true;
            DataBinding();          
        }


        /// <summary>
        /// This method manage the page changing on the bottom of result's panel.
        /// </summary>
        protected void PageChanged(object sender, GridViewPageEventArgs e)
        {
            gv_result.PageIndex = e.NewPageIndex;
            DataBinding();
        }


        /// <summary>
        /// If you click on item that rapresent the user founded, you are redirect on userProfile page if you are logged in.
        /// </summary>
        protected void Index_changing(object sender, GridViewSelectEventArgs e)
        {
            DataKey valuesToSend = gv_result.DataKeys[e.NewSelectedIndex];
            if (Page.User.Identity.IsAuthenticated)
                Response.Redirect("secure/userProfile.aspx?Nick=" + valuesToSend.Values[0].ToString());
            else
                Response.Redirect("NotAuthorized.aspx");
        }

        /// <summary>
        /// Gets a string that represents the complete url of userProfile page.
        /// </summary>
        protected String getUrlProfile(string userName)
        {
            if (Page.User.Identity.IsAuthenticated)
                return "secure/userProfile.aspx?Nick=" + userName;
            else
                return "NotAuthorized.aspx";
        }



        /// <summary>
        /// Gets a string that represents gender of selected user.
        /// </summary>
        protected String Get_Gender(String gender)
        {
            if (gender.Equals("Female")) return "female";
            else if (gender.Equals("Male")) return "male";
            else return "unknow";
        }

        /// <summary>
        /// Gets a string to know if selected user is smoker on not.
        /// </summary>
        protected String Get_Smoker(String gender)
        {
            if (gender.Equals("Smoker")) return "smoker";
            else if (gender.Equals("NotSmoker")) return "no_smoker";
            else return "unknow";
        }
        

    }
}
